clear
clear matrix
set more off
set mem 600m
set matsize 800

********************************************************************************

use regression10, clear

preserve
* realquartile
use datacdv2, clear
keep id_prof period quartile*
collapse quartile*, by(id_prof period)
tab quartile1e 
tab quartile1b
ren quartile1e realquartile1e
ren quartile1b realquartile1b
sort id_prof period
save temp, replace
restore
sort id_prof period
merge 1:1 id_prof period using temp
drop if _merge!=3
drop _merge
tab realquartile1b, g(rq1b)
tab realquartile1e, g(rq1e)


* quartile
tab quartile1b, g(q1b)
tab quartile1e, g(q1e)
tab period, g(p)

g refruleass=reform*ruleR
g refruleord=reform*ruleA

g lKass=log(Kass)
g lKord=log(Kord)

g lNass=log(Nass)
g lNord=log(Nord)

gen lioutput1e = ln(100*ioutput1e + 1)
gen lioutput1b = ln(100*ioutput1b + 1)

gen ass_1=ricass
gen ord_1=assord
gen fascass=fasc1
gen fascord=fasc2

set matsize 10000
qui tab ssd, gen(sd)
qui tab id_uni, gen(uni)
* drop univ molise and chim/05 to avoid sparse variables
drop sd44 uni36

/****************************************************************************************************************
										***** MAIN REGRESSION  *****
****************************************************************************************************************/
**** main table: (associate) and (full)
**** dependent variable ioutput1e
**** interaction effects and sample split by subject - decomposing K and N - fixed effects - quartile are asymmetric 
**** contains as additional controls ext avage shfemale p2-p5  
/****************************************************************************************************************/

set more off

********** if you want orderliness from 0 to 1 rather than from -1 to 1 ********
********** set M01 = 1 ****
scalar M01 = 0
if M01 == 1{
foreach mm in mMordE mMordB mMassE sdMassB {
replace `mm' = 1/2 +`mm'/2
}
}
****************************************************

**** generate two new continuous variables   ******
foreach fascia in ass ord{
	gen lkK`fascia' =  lK`fascia'*refrule`fascia'
}
foreach fascia in ass ord{
	gen lnN`fascia' =  lN`fascia'*refrule`fascia'
}


************************ GGGG **********************
foreach fascia in ass ord{
	egen e`fascia' = sd(ssdcodefinal) if fasc`fascia'>0|`fascia'_1==1 , by( id_prof )
	replace e`fascia' = 0 if e`fascia'==.
	replace e`fascia' = 1 if e`fascia'>0
}
**************************************************** 
saveold regFinal , replace

 
************************************
************* MAIN TABLE ***********
********************************************************************************
*******************************************************************************************************************
*************           now regression with the K  or N coefficient altered after 
*************           the reform according to the intensity of the treatment
*************           These are column 1 & 2 in main table
*******************************************************************************************************************

use regFinal , clear

* Tab 4 - Base Regression: coauthors weighting
set more off
global tavola = "c.mM#i.quartile1 c.lK#i.quartile1 c.lN#i.quartile1 c.lnN#i.quartile1  ext avage shfemale homo1 homo3 p2-p5 "
foreach fascia in ass ord{
foreach cluster in ssd {
preserve 
	ren quartile1e quartile1
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "(fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0"
	xtreg ioutput1e $tavola sd1-sd369 uni1-uni90 if $cond , fe vce(cluster  `cluster') 
	parmest, saving("beta_`fascia'.dta", replace)
	predict indiv_fe_`fascia', u
	outreg2 using table_4.xls, keep($tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' output1e) label append
	save tmp_`fascia', replace
restore
	}
}

*predict u, 
********************************************************************************
** this bit stores the fixed effects 
********************************************************************************
* gen three files
*1 .dta individuals fe [datasets indiv_fe_ass.dta e indiv_fe_ord.dta ]
*1 .dta university fe [datasets beta_ass.dta e beta_ord.dta]
*1 .dta sector fe [datasets beta_ass.dta e beta_ord.dta]

set scheme lean2
foreach fascia in ass ord{
preserve 
use tmp_`fascia', clear
drop if indiv_fe_`fascia'==.
bys id_prof: gen a=_n
keep if a==1
keep id_prof indiv_fe_`fascia' subject female
save indiv_fe_`fascia', replace
restore
}
preserve
use indiv_fe_ass, clear
merge 1:1 id_prof using indiv_fe_ord
drop _merge
drop if indiv_fe_ord>5
drop if indiv_fe_ass>5
twoway (scatter indiv_fe_ord indiv_fe_ass if subject==1 , msize(vtiny)) (function y=x, lpattern(dash) range(-5.0 5.0)) , xline(0) xlabel(-5(5)5) ylabel(-5(5)5) yline(0) xtitle("") ytitle("") title(Science) name(g1, replace)  legend(off) graphregion(color(white))
twoway (scatter indiv_fe_ord indiv_fe_ass if subject==2 , msize(vtiny) )  (function y=x, lpattern(dash) range(-5.0 5.0)) , xline(0) xlabel(-5(5)5) ylabel(-5(5)5) yline(0) xtitle("") ytitle("") title(Medicine) name(g2, replace) legend(off) graphregion(color(white))
twoway (scatter indiv_fe_ord indiv_fe_ass if subject==3 , msize(vtiny) )  (function y=x, lpattern(dash) range(-5.0 5.0)) , xline(0) xlabel(-5(5)5) ylabel(-5(5)5) yline(0) xtitle("") ytitle("") title(Engineering) name(g3, replace)  legend(off) graphregion(color(white))
twoway (scatter indiv_fe_ord indiv_fe_ass if subject==4 , msize(vtiny) )  (function y=x, lpattern(dash) range(-5.0 5.0)) , xline(0) xlabel(-5(5)5) ylabel(-5(5)5) yline(0) xtitle("") ytitle("") title(Humanities & Law) name(g4, replace)  legend(off) graphregion(color(white))
twoway (scatter indiv_fe_ord indiv_fe_ass if subject==5 , msize(vtiny) )  (function y=x, lpattern(dash) range(-5.0 5.0)) , xline(0) xlabel(-5(5)5) ylabel(-5(5)5) yline(0) xtitle("") ytitle("") title(Social Sciences) name(g5, replace)  legend(off) graphregion(color(white))
twoway (scatter indiv_fe_ord indiv_fe_ass, msize(vtiny)) (function y=x, lpattern(dash) range(-5.0 5.0)) , xline(0) xlabel(-5(5)5) ylabel(-5(5)5) yline(0) xtitle("") ytitle("") title(Whole Sample) name(g6, replace)  legend(off) graphregion(color(white))

* Figure 9 - Individual Fixed Effects for two Promotions
graph combine g1 g2 g3 g4 g5 g6, cols(3) ycommon xcommon l1(Individual fixed effect (Promotion to Full Professor)) b1(Individual fixed effect (Promotion to Associate)) graphregion(color(white))
restore

********************************************************************************

*******************************************************************************************************************
*************           the same regression as above, but with a different measure for output (and the index x) 
*************           the reform according to the intensity of the treatment
*************           These are columns "Output weighted with impacy factor" in Table 4
*******************************************************************************************************************

set more off
global tavola = "c.mM#i.quartile1 c.lK#i.quartile1 c.lN#i.quartile1 c.lnN#i.quartile1  ext avage shfemale homo1 homo3 p2-p5 "
foreach fascia in ass ord{
foreach cluster in ssd {
preserve 
	ren quartile1b quartile1
	ren mM`fascia'B mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "((fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0)"

	xtreg ioutput1e $tavola sd1-sd369 uni1-uni90 if $cond, fe vce(cluster  `cluster') 
	parmest, saving("beta_`fascia'.dta", replace)
	predict indiv_fe_`fascia', u
	outreg2 using table_4.xls, keep($tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' output1e) label append
	save tmp_`fascia', replace
restore
	}
}

**************************************************************************************************************************
* robustness check: using future values - Table A11 - columns "Future Values" clustering at Scientific Sector 
**************************************************************************************************************************
set more off
global tavola = "c.mM#i.quartile1 c.lK#i.quartile1 c.lN#i.quartile1 c.lnN#i.quartile1  ext avage shfemale homo1 homo3 p2-p5 "
foreach fascia in ass ord{	
preserve
	gen tmp=f.mM`fascia'E 
	drop mM`fascia'E
	ren tmp mM`fascia'E
	gen tmp=f.lK`fascia'
	drop lK`fascia'
	ren tmp lK`fascia'
	gen tmp=f.lN`fascia' 
	drop lN`fascia'
	ren tmp lN`fascia'

	gen tmp=f.lnN`fascia' 
	drop lnN`fascia'
	ren tmp lnN`fascia'
	
	ren quartile1e quartile1
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lN`fascia'  lN
	ren lnN`fascia'  lnN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "((fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0)"
	xtreg ioutput1e $tavola sd1-sd369 uni1-uni90 if $cond, fe vce(cluster ssd) 
	outreg2 using table_4.xls, keep($tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' future values output1e) label append
restore
}

***********************************************************************************************************
*****          IV regression with  sd.dev output of full professor as instrument for M         ************
*****          
*****          These are columns "Instrumental Variables: homogeneity" in Table 4		       ************
***********************************************************************************************************

set more off
set matsize 10000
global tavola = "lNq1e1-lNq1e4 lKq1e1-lKq1e4 lnNq1e1-lnNq1e4 ext avage shfemale homo1 homo3 p2-p5 "
foreach instru in "sd3q1e1-sd3q1e4 pyrq1e1-pyrq1e4" {
foreach fascia in ass ord{	
preserve
	ren quartile1e quartile1
	ren ext`fascia' ext
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN

	g mMEq1e1=mM*q1e1
	g mMEq1e2=mM*q1e2
	g mMEq1e3=mM*q1e3
	g mMEq1e4=mM*q1e4

	su mMEq1e1 mMEq1e2 mMEq1e3 mMEq1e4 if (fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0
	
	g lKq1e1=lK*q1e1
    g lKq1e2=lK*q1e2
    g lKq1e3=lK*q1e3
    g lKq1e4=lK*q1e4

	su lKq1e1 lKq1e2 lKq1e3 lKq1e4 if (fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0
	
	g lkKq1e1=lkK*q1e1
    g lkKq1e2=lkK*q1e2
    g lkKq1e3=lkK*q1e3
    g lkKq1e4=lkK*q1e4

	g lnNq1e1=lnN*q1e1
    g lnNq1e2=lnN*q1e2
    g lnNq1e3=lnN*q1e3
    g lnNq1e4=lnN*q1e4

	su lnNq1e1 lnNq1e2 lnNq1e3 lnNq1e4 if (fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0
	
	g lNq1e1=lN*q1e1
    g lNq1e2=lN*q1e2
    g lNq1e3=lN*q1e3
    g lNq1e4=lN*q1e4	
	
	su lNq1e1 lNq1e2 lNq1e3 lNq1e4 
	
	g sd3q1e1=sd3e*q1e1
	g sd3q1e2=sd3e*q1e2
	g sd3q1e3=sd3e*q1e3
	g sd3q1e4=sd3e*q1e4
	
	su sd3q1e1 sd3q1e2 sd3q1e3 sd3q1e4 if (fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0
	
	g pyrq1e1=pyramid`fascia'*q1e1
	g pyrq1e2=pyramid`fascia'*q1e2
	g pyrq1e3=pyramid`fascia'*q1e3
	g pyrq1e4=pyramid`fascia'*q1e4
	
	su pyrq1e1 pyrq1e2 pyrq1e3 pyrq1e4 if (fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0
	
	global cond = "(fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0"
	global strumento = "`instru'"
	global depvar = "mMEq1e1 mMEq1e2 mMEq1e3 mMEq1e4" 
	

* (First stage regressions: interaction of index x with partition Table A12)	
xtivreg2 ioutput1e ($depvar = $strumento) $tavola sd1-sd369 uni1-uni90 if $cond , fe savefirst first  
est restore _xtivreg2_mMEq1e1 
outreg2 using table_4_IV_`fascia'.xls, replace
est restore _xtivreg2_mMEq1e2 
outreg2 using table_4_IV_`fascia'.xls, append
est restore _xtivreg2_mMEq1e3
outreg2 using table_4_IV_`fascia'.xls, append
est restore _xtivreg2_mMEq1e4
outreg2 using table_4_IV_`fascia'.xls, append


xtivreg2 ioutput1e ($depvar = $strumento) $tavola sd1-sd369 uni1-uni90 if $cond , fe 
outreg2 using table_4_IV.xls, keep($depvar $tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' xtivreg output1e) append	
restore
}
}
log close

** This is table A10 "Results of base regressions, coauthors weighting, by broad subject area"
set more off
recode subject 1=3 2=5 3=7 4=11 5=13
label define subject 3 "science" 5 "medicine" 7 "engineering" 11 "humanities law" 13 "social sciences" , replace
foreach x of numlist 1 3 5 7 11 13 {
foreach fascia in ass ord{
foreach cluster in ssd  {
preserve 
	ren quartile1e quartile1
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "(fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0"

	xtreg ioutput1e c.mM#i.quartile1 c.lK#i.quartile1 c.lN#i.quartile1 c.lnN#i.quartile1 ext avage shfemale homo1 homo3 p2-p5 sd1-sd369 uni1-uni90 if mod(subject,`x')==0 & $cond, fe vce(cluster  `cluster') 
	parmest, saving("beta_`fascia'.dta", replace)
	predict indiv_fe_`fascia', u
	outreg2 using tabA10_bySubject.xls,  noparen aster(coef) se bdec(3) nocons cti(`fascia' output1e) label append
	save tmp_`fascia', replace
restore
	}
}
}

xtset id_prof period 
by id_prof: egen a=count(ssdcodefinal) 
by id_prof: gen change=ssdcodefinal!=f1.ssdcodefinal 
by id_prof: gen d=_n 
replace change=0 if change==1 & d==a
by id_prof: egen e=max(change)

* regressions to draw the decile tables *
* resulting coefficients are stores in dec_ass.dta and dec_ord.dta files *

set more off
set matsize 800
cap drop dece*
replace decile1e=1 if decile1e<=1.5&decile1e!=.
replace decile1e=2 if decile1e>1.5&decile1e<=2.5
replace decile1e=3 if decile1e>2.5&decile1e<=3.5
replace decile1e=4 if decile1e>3.5&decile1e<=4.5
replace decile1e=5 if decile1e>4.5&decile1e<=5.5
replace decile1e=6 if decile1e>5.5&decile1e<=6.5
replace decile1e=7 if decile1e>6.5&decile1e<=7.5
replace decile1e=8 if decile1e>7.5&decile1e<=8.5
replace decile1e=9 if decile1e>8.5&decile1e<=9.5
replace decile1e=10 if decile1e>9.5&decile1e!=.
tab decile1e, g(dece)

* Creates .dta with coefficients and std.errors obtained from xtreg

foreach fascia in ass ord{
preserve
global cond = "(fasc`fascia'>0|`fascia'_1==1)"
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
xtreg ioutput1e c.mM#i.decile1e c.lK#i.decile1e c.lN#i.decile1e c.lnN#i.decile1e ext avage shfemale homo1 homo3 p2-p5 sd1-sd369 uni1-uni90 if $cond, fe vce(cluster id_prof) dfadj
matrix b=e(b)
matrix c=b'
matrix define b2=e(V)
matrix b3 = vecdiag(b2)
matrix b4=b3'
svmat double b4, name(se1)
svmat double c, name(col1)
mkmat col1 se1 if col1!=. & se1!=.
keep se11 col11
drop if _n>50

lincom 1b.decile1e#c.lN + 2*1b.decile1e#c.lnN
global va1 = r(estimate)
global va2 = r(se)
replace col11 = $va1 if _n==41
replace se11  = $va2 if _n==41

forvalues j = 2(1)10 {
qui lincom `j'.decile1e#c.lN + 2*`j'.decile1e#c.lnN
qui global va1 = r(estimate)
qui global va2 = r(se)
replace col11 = $va1 if _n==`j' + 40
replace se11  = $va2 if _n==`j' + 40
}

gen col2=col11
gen col3=col11
gen col4=col11
gen col5=col11
gen se2=se11
gen se3=se11
gen se4=se11
gen se5=se11
replace col2= col2[_n+10] if _n<11
replace col3= col3[_n+20] if _n<11
replace col4= col4[_n+30] if _n<11
replace col5= col5[_n+40] if _n<11
replace se2= se2[_n+10] if _n<11
replace se3= se3[_n+20] if _n<11
replace se4= se4[_n+30] if _n<11
replace se5= se5[_n+40] if _n<11
drop if _n>10
rename col11 cm`fascia'
rename col2 ck`fascia'
rename col3 cn`fascia'
rename col4 crefrule`fascia'
rename col5 cNref`fascia'
rename se11 sem`fascia'
rename se2 sek`fascia'
rename se3 sen`fascia'
rename se4 serefrule`fascia'
rename se5 seNref`fascia'
replace sem`fascia'=sqrt(sem`fascia')
replace sek`fascia'=sqrt(sek`fascia')
replace sen`fascia'=sqrt(sen`fascia')
replace serefrule`fascia'=sqrt(serefrule`fascia')
replace seNref`fascia'=sqrt(seNref`fascia')
gen decile=_n
save dec_`fascia', replace
restore
}

use dec_ass, clear
sort decile
merge 1:1 decile using dec_ord
drop _merge
saveold decileV13, replace
************************************************************************
*** 					Index M (Figure 6) 							 ***
************************************************************************
use decileV13 , clear
label var decile "Decile of ability"

gen cmassP = cmass + 1.96*semass
gen cmassM = cmass - 1.96*semass

gen cmordP = cmord + 1.96*semord
gen cmordM = cmord - 1.96*semord

label var cmass "Assistant Professors"
label var cmord "Associate Professors"

set scheme lean2
#delimit ;
twoway function y=0 , range(1 10) lpattern(l ) lwidth(thin ) lcolor( gs2) ||
line cmass cmassP cmassM cmord cmordP cmordM decile , 
lpattern(l - - l - - ) lwidth(medium thin thin medthick medthin medthin ) lcolor( /*edkblue edkblue  edkblue*/ gs2 gs2 gs2 /*cranberry cranberry cranberry*/ gs11 gs11 gs11 gs2)
saving(M.gph, replace) 
legend(order(2 5) rowgap(0) position(10) ring(0) size(medsmall) nobox symxsize(13) symysize(2) colfirst cols(1)) 
ytitle("Value of coefficient", size(medsmall)) 
xtitle("Academic's decile in the period output ranking", size(medsmall)) 
xlabel(,labsize(vsmall)) xlabel(, valuelabel) xlabel(, angle(horizontal)) xlabel(1(1)10) ylabel(-.4(.2)1) 
ylabel(,labsize(vsmall)) ylabel(, angle(horizontal)) graphregion(color(white)) 
;
#delimit cr

graph export "$path\Graph_fig6.pdf" , replace
************************************************************************
*** 			N: Number of competiors Assistant (Figure 7) 		 ***
************************************************************************
label var cNreford "       Post-Reform Constraint tight"
label var cnord "Associate Professors: Pre-Reform"

label var cNrefass "       Post-Reform Constraint tight"
label var cnass "Assistant Professors: Pre-Reform"

set scheme lean2
#delimit ;
twoway function y=0 , range(1 10) lpattern(l ) lwidth(thin ) lcolor( gs2) ||
line cnord  cNreford cnass  cNrefass  decile if cnass<1000, 
lpattern( _ l _ l) lwidth( medthick medthick medium  medium   ) lcolor( gs2 gs2 /*cranberry  cranberry*/ gs11 gs11 /*edkblue edkblue*/ )
saving(N.gph, replace) 
legend(order( 2 3 4 5) rowgap(0) position(10) ring(0) size(medsmall) nobox symxsize(13) symysize(2) colfirst cols(1)) 
ytitle("Value of coefficient", size(medsmall)) 
xtitle("Academic's decile in the period output ranking", size(medsmall)) 
xlabel(,labsize(vsmall)) xlabel(, valuelabel) xlabel(, angle(horizontal)) xlabel(1(1)10)  
ylabel(,labsize(vsmall)) ylabel(, angle(horizontal)) ylabel(-0.1(0.05)0.25)  graphregion(color(white))
;
#delimit cr

graph export "$path\Graph_fig7.pdf" , replace
************************************************************************
*** 				K: Number of posts (Figure 8) 					 ***
************************************************************************
gen ckassP = ckass + 1.96*senass
gen ckassM = ckass - 1.96*senass

gen ckordP = ckord + 1.96*senord
gen ckordM = ckord - 1.96*senord

label var ckass "Assistant Professors"
label var ckord "Associate Professors"

set scheme lean2
#delimit ;
twoway function y=0 , range(1 10) lpattern(l ) lwidth(thin ) lcolor( gs2) ||
line ckass ckassP ckassM ckord ckordP ckordM decile if ckass<1000, 
lpattern(l - - l - - ) lwidth(medium thin thin medthick medthin medthin ) lcolor( /*edkblue  edkblue  edkblue*/ gs2 gs2 gs2 /*cranberry cranberry cranberry*/ gs11 gs11 gs11 gs2)
saving(K.gph, replace) 
legend(order(2 5) rowgap(0) position(2) ring(0) size(medsmall) nobox symxsize(11) symysize(2) colfirst cols(1)) 
ytitle("Value of coefficient", size(medsmall)) 
xtitle("Academic's decile in the period output ranking", size(medsmall)) 
xlabel(,labsize(vsmall)) xlabel(, valuelabel) xlabel(, angle(horizontal)) xlabel(1(1)10) ylabel(-.1(.05).1) 
ylabel(,labsize(vsmall)) ylabel(, angle(horizontal)) graphregion(color(white)) 
;
#delimit cr

graph export "$path\Graph_fig8.pdf" , replace
************************************************************************
***						Figure 3 - left panel					     ***	
************************************************************************
use "datacdv2.dta", clear

local kA = 5 
local kAA = 12
local kAAA = 2000

gen ruleR=0 if k1<=`kA'&(fascia==1|ricass==1)
replace ruleR=1 if (`kA'<k1)&(k1<=`kAA')&(k1!=.)&(fascia==1|ricass==1)
replace ruleR=2 if (`kAA'<k1)&(k1<=`kAAA')&(k1!=.)&(fascia==1|ricass==1)
replace ruleR=4 if (`kAAA'<k1)&(k1!=.)&(fascia==1|ricass==1)

gen ruleA=0 if k1<=`kA'&(fascia==2|assord==1)
replace ruleA=1 if (`kA'<k1)&(k1<=`kAA')&(k1!=.)&(fascia==2|assord==1)
replace ruleA=2 if (`kAA'<k1)&(k1<=`kAAA')&(k1!=.)&(fascia==2|assord==1)
replace ruleA=4 if (`kAAA'<k1)&(k1!=.)&(fascia==2|assord==1)

preserve
tab ruleR, gen(RA)
tab ruleA, gen(RO)
collapse (sum) RA1 RA2 RA3 RO1 RO2 RO3, by(ssd period)
collapse (sum) RA1 RA2 RA3 RO1 RO2 RO3, by(period)

label variable RA1 "pippo"

set scheme lean2
#delimit  ;
graph bar RA1 RA2 RA3 RO1 RO2 RO3 if period>2, over(period) aspect(.4) 
bar(4, color(/*226 74 105*/ gs2)) 
bar(5, color(/*189 55 82*/ gs4)) 
bar(6, color(/*152 30 54*/ gs8)) 
bar(1, color(/*63 131 199*/ gs10)) 
bar(2, color(/*42 94 166*/ gs12)) 
bar(3, color(/*18 82 146*/ gs14)) 
graphregion(color(white)) bgcolor(white) 
legend(label(1 "Assistant Prof: 0") label(2 "Assistant Prof: 1") label(3 "Assistant Prof: 2") label(4 "Associate Prof: 0") label(5 "Associate Prof: 1") label(6 "Associate Prof: 2") cols(3) lw(none))
ylabel( , angle(0)) ;
graph export  "$path\Graph_fig3_left.pdf", replace
restore
************************************************************************
***						Figure 3 - right panel						 ***	
************************************************************************
use "datacdv2.dta", clear

local kA = 5 
local kAA = 12
local kAAA = 2000

gen ruleR=0 if k1<=`kA'&(fascia==1|ricass==1)
replace ruleR=1 if (`kA'<k1)&(k1<=`kAA')&(k1!=.)&(fascia==1|ricass==1)
replace ruleR=2 if (`kAA'<k1)&(k1<=`kAAA')&(k1!=.)&(fascia==1|ricass==1)
replace ruleR=4 if (`kAAA'<k1)&(k1!=.)&(fascia==1|ricass==1)

gen ruleA=0 if k1<=`kA'&(fascia==2|assord==1)
replace ruleA=1 if (`kA'<k1)&(k1<=`kAA')&(k1!=.)&(fascia==2|assord==1)
replace ruleA=2 if (`kAA'<k1)&(k1<=`kAAA')&(k1!=.)&(fascia==2|assord==1)
replace ruleA=4 if (`kAAA'<k1)&(k1!=.)&(fascia==2|assord==1)
preserve
drop if anno < 1999
set scheme lean2
collapse (mean) ruleA ruleR (sd) sdA=ruleA sdR=ruleR, by(anno)
twoway (scatter ruleA anno, msize(medium-small) msymbol(Oh)) (scatter ruleR anno, msize(medium-small) msymbol(Dh)), xtitle("") xlabel(1999(1)2011, angle(45)) ylabel(, angle(0)) aspect(.4) graphregion(color(white)) legend(label(1 "Assistant. Prof") label(2 "Associate. Prof") position(2) ring(0) col(1)) 
graph export  "$path\Graph_fig3_right.pdf", replace
************************************************************************
***						Figure A1 pag A12					         ***	
************************************************************************
use Mass2extended, clear  /*run meritp2.do to get Mass2extended.dta*/ 
replace mMassE = 1/2 +mMassE/2
gen biblio=(area==1|area==2|area==3|area==4|area==5|area==6|area==7|area==9|ssd=="ICAR/01"|ssd=="ICAR/02"|ssd=="ICAR/03"|ssd=="ICAR/04"|ssd=="ICAR/05"| ///
ssd=="ICAR/06"|ssd=="ICAR/07"|ssd=="ICAR/08"|ssd=="ICAR/09"|ssd=="ICAR/22"| ///
ssd=="M-PSI/01"|ssd=="M-PSI/02"|ssd=="M-PSI/03"|ssd=="M-PSI/04"|ssd=="M-PSI/05"|ssd=="M-PSI/06"|ssd=="M-PSI/07"|ssd=="M-PSI/08")

label define areacun 1 "mathematics and computer science" 2 "physics" 3 "chemistry" 4 "earth science" 5 "biology" ///
6 "medicine" 7 "agriculture and veterinary sciences" 8 "engineering and architecture(biblio)" 15 "architecture (not biblio)" 9 "industrial engineering and ICT" ///
10 "humanities" 11 "history, philosophy" 16 "psichology" 12 "law" 13 "economics and statistics" 14 "sociology and political science" 99 "missing"
label value areacun areacun

g KsuN=Kass/Nass

table period subject if period>0, c(m mMassE sd mMassE m mMassB sd mMassB m extass) row col format(%9.3f)
table area period if period>0&area<15, c(m mMassE sd mMassE m mMassB sd mMassB m extass) row col format(%9.3f)
table period biblio if period>0, c(m mMassE sd mMassE m mMassB sd mMassB m extass) row col format(%9.3f)

gen areabis=areacun
replace areabis=15 if ssd=="ICAR/10"|ssd=="ICAR/11"|ssd=="ICAR/12"|ssd=="ICAR/13"|ssd=="ICAR/14"|ssd=="ICAR/15"|ssd=="ICAR/16"|ssd=="ICAR/17"|ssd=="ICAR/18"| ///
ssd=="ICAR/19"|ssd=="ICAR/20"|ssd=="ICAR/21"
replace areabis=16 if ssd=="M-PSI/01"|ssd=="M-PSI/02"|ssd=="M-PSI/03"|ssd=="M-PSI/04"|ssd=="M-PSI/05"|ssd=="M-PSI/06"|ssd=="M-PSI/07"|ssd=="M-PSI/08"
label value areabis areacun

**** table by area
table areabis period if period>0&areabis<20, c(m mMassE sd mMassE n mMassE) row col format(%9.3f)
drop if period==0
**** graph 
twoway (kdensity mMassE if areabis==1, lcolor(gs11)) (kdensity mMassE if areabis==2, lcolor(gs11)) (kdensity mMassE if areabis==3, lcolor(gs11)) (kdensity mMassE if areabis==4, lcolor(gs11)) ///
(kdensity mMassE if areabis==5, lcolor(gs11)) (kdensity mMassE if areabis==6, lcolor(gs11)) (kdensity mMassE if areabis==7, lcolor(gs11)) (kdensity mMassE if areabis==8, lcolor(gs11)) /// 
(kdensity mMassE if areabis==9, lcolor(gs11)) (kdensity mMassE if areabis==16, lcolor(gs11))(kdensity mMassE if areabis==15, lcolor(gs2) lpattern(dash)) (kdensity mMassE if areabis==10, lcolor(gs11) lpattern(dash)) (kdensity mMassE if areabis==11, lcolor(gs2) lpattern(dash)) (kdensity mMassE if areabis==12, lcolor(gs2) lpattern(dash)) ///
(kdensity mMassE if areabis==13, lcolor(gs2) lpattern(dash) lwidth(thick)) (kdensity mMassE if areabis==14, lcolor(gs2) lpattern(dash)), legend(order(1 "Bibliometric" 11 "Non-bibliometric" 15 "Econ & Stat") cols(1) ring(0) pos(2)) xtitle(Index of Orderliness) ytitle(Kernel density) graphregion(color(white))
graph save Graph "$path/merit_graph.pdf", replace
************************************************************************
***						Figure A2 pag A15						     ***
************************************************************************
use people.dta, clear
gen tmp=1
collapse (sum) tmp if (fascia==3), by(ssd anno) 
kdensity tmp if tmp<450, graphregion(color(white)) title("") xtitle(Number of Full Professors) ytitle(Fraction of Scientific Sectors) note("")
graph save Graph "$path/graph_A2.pdf", replace
************************************************************************
***						Figure A3 pag A17						     ***
************************************************************************
use people.dta, clear
keep if anno<2012
by id_prof: egen age_max=max(age)
by id_prof: gen a=_n
keep if age==age_max
drop if anno==2011
gen tmp=1
set scheme s1mono
twoway (kdensity age_max if (fascia==1 | fascia==2) & female==1, n(20) lcolor( gs2)) (kdensity age_max  if (fascia==1 | fascia==2) & female==0, n(20) lwidth(thick ) lcolor( gs2)) (kdensity age_max if (fascia==3) & female==1, n(20) lcolor(gs11) lpattern(longdash)) (kdensity age_max if (fascia==3) & female==0, n(20) lwidth(thick ) lcolor(gs11) lpattern(longdash)), graphregion(color(white))  ytitle(Fractions of Academics leaving) xtitle(Age at exit from the dataset) legend(order(1 "Female Associate and Assistant Professor" 2 "Male Associate and Assistant Professor" 3 "Female Full Professor" 4 "Male Full Professor") rowgap(0) position(11) ring(0) size(vsmall) nobox symxsize(30) symysize(4) colfirst cols(1))
graph save Graph "$path/graph_A3.pdf", replace
************************************************************************
***						Tables A13 and A14						     ***
************************************************************************
preserve
bys id_unifinal: gen p=_n
keep if p==1
keep id_unifinal regionfinal
drop if regionfinal==.
save uni, replace
restore
use beta_ass, clear
rename estimate estimate_ass
keep parm estimate_ass p
rename p p_ass
merge 1:1 parm using beta_ord
rename estimate estimate_ord
rename p p_ord
keep parm estimate_ass p_ass estimate_ord p_ord
gen a=strpos(parm, "uni")
tab a
keep if a==1
sort parm
drop a
gen a=parm
replace a=subinstr(a, "uni", "",1)
sort a
destring a, replace
sort a
rename a id_unifinal
merge 1:1 id_unifinal using uni, update

decode id_unifinal , ge(id_stringa)

drop if _merge==2
drop _merge
sort id_stringa
replace id_stringa=proper(id_stringa)

decode regionfinal , ge(region_stringa)
replace region_stringa=proper(region_stringa)
order region_stringa id_stringa estimate_ass p_ass estimate_ord p_ord
keep region_stringa id_stringa estimate_ass p_ass estimate_ord p_ord

export excel using "$path\tables_A13_A14.xls", firstrow(variables) replace

************************************************************************
***					Tables A11 columns 1-2-3-4-5-6		    		 ***
************************************************************************
set more off
global tavola = "c.mM#i.quartile1 c.lK#i.quartile1 c.lN#i.quartile1 c.lnN#i.quartile1  ext avage shfemale homo1 homo3 p2-p5 "
foreach fascia in ass ord{
foreach cluster in id_prof {
preserve 
	ren quartile1e quartile1
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "(fasc`fascia'>0|`fascia'_1==1)"
	xtreg ioutput1e $tavola sd1-sd369 uni1-uni90 if $cond , fe vce(cluster  `cluster') 
	parmest, saving("beta_`fascia'.dta", replace)
	predict indiv_fe_`fascia', u
	outreg2 using tabA11.xls, keep($tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' output1e) label append
	save tmp_`fascia', replace
restore
	}
}
foreach fascia in ass ord{
foreach cluster in   ssd {
preserve 
	ren quartile1e quartile1
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "(fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0"
	xtreg ioutput1e $tavola sd1-sd369 uni1-uni90 if $cond , fe vce(cluster  `cluster')  
	parmest, saving("beta_`fascia'.dta", replace)
	predict indiv_fe_`fascia', u
	outreg2 using tabA11.xls, keep($tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' output1e) label append
	save tmp_`fascia', replace
restore
	}
}
foreach fascia in ass ord{
foreach cluster in id_prof {
preserve 
	ren quartile1e quartile1
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "(fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0"
	xtreg ioutput1e $tavola sd1-sd369 uni1-uni90 if $cond , fe vce(cluster  `cluster')  
	parmest, saving("beta_`fascia'.dta", replace)
	predict indiv_fe_`fascia', u
	outreg2 using tabA11.xls, keep($tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' output1e) label append
	save tmp_`fascia', replace
restore
	}
}

************************************************************************
***							 RESTAT - R1 							 ***
************************************************************************
use regFinal, clear

************************************************************************
***						 Figures A4 and A5 						     ***
************************************************************************
** 1.i take the nine SSD which are at the interior deciles (p10,p20,..., p80, p90) by size in period 1, and plot them over time

use regFinal, clear

********************
*  Associate Prof  *
********************
collapse Nass Kass mMassE Nord Kord mMordE, by(ssd period)
ren mMassE Mass
ren mMordE Mord

xtile xNass=Nass if period==1, nq(10)
egen tmp1Nass=max(Nass) if period==1&Nass!=., by(xNass)
g tmp2Nass=(tmp1Nass==Nass) if Nass!=.
egen tmp3Nass=max(tmp2Nass), by(ssd)
egen tmp4Nass=count(Nass) if Nass!=., by(ssd)
gen tmp5Nass=tmp4Nass==5
egen sNass=sd(Nass), by(ssd)
egen tmp6Nass=min(sNass) if period==1&tmp3Nass==1&tmp5Nass==1, by(xNass)
g tmp7Nass=(sNass==tmp6Nass) if sNass!=.
egen tmp8Nass=max(tmp7Nass), by(ssd)
tab tmp8Nass
table ssd period if tmp8Nass==1, c(m Nass)
table ssd period if tmp8Nass==1, c(m Kass)
table ssd period if tmp8Nass==1, c(m Mass)

keep if tmp8Nass==1
egen tmp9Nass=max(xNass), by(ssd)
replace xNass=tmp9Nass
tab xNass, m

line Nass period if xNass==1 , lcolor( "163 181 253") || ///
line Nass period if xNass==2 , lcolor( "130 152 243") || ///
line Nass period if xNass==3 , lcolor( "104 129 230") || ///
line Nass period if xNass==4 , lcolor( "73 101 212") || ///
line Nass period if xNass==5 , lcolor( "51 79 192") || ///
line Nass period if xNass==6 , lcolor( "31 58 165") || ///
line Nass period if xNass==7 , lcolor( "17 41 137") || ///
line Nass period if xNass==8 , lcolor( "8 27 102") || ///
line Nass period if xNass==9 , lcolor( "2 14 63") ///
, title("Assistant professors")  ytitle("Number of competitors, {it:N}", size(medium)) ///
xtitle("", size(small)) yscale(log) ylabel(10 50 100  200 ,labsize(small) nogrid) ///
xlabel(none) xtick(1 (1) 5 , grid) ysize(2.3) xsize(2.5)  ///
legen(off) saving(trendNass, replace) graphregion(color(white))

line Kass period if xNass==1 , lcolor( "163 181 253") || ///
line Kass period if xNass==2 , lcolor( "130 152 243") || ///
line Kass period if xNass==3 , lcolor( "104 129 230") || ///
line Kass period if xNass==4 , lcolor( "73 101 212") || ///
line Kass period if xNass==5 , lcolor( "51 79 192") || ///
line Kass period if xNass==6 , lcolor( "31 58 165") || ///
line Kass period if xNass==7 , lcolor( "17 41 137") || ///
line Kass period if xNass==8 , lcolor( "8 27 102") || ///
line Kass period if xNass==9 , lcolor( "2 14 63") ///
, ytitle("Number of posts, {it:K}", size(medium)) ///
xtitle("", size(small)) yscale(log) ylabel(5 10 20 40 80 ,labsize(small) nogrid) ///
xlabel(none) xtick(1 (1) 5 , grid) ysize(2.3) xsize(2.5) ///
legen(off) saving(trendKass, replace) graphregion(color(white))

line Mass period if xNass==1 , lcolor( "163 181 253") || ///
line Mass period if xNass==2 , lcolor( "130 152 243") || ///
line Mass period if xNass==3 , lcolor( "104 129 230") || ///
line Mass period if xNass==4 , lcolor( "73 101 212") || ///
line Mass period if xNass==5 , lcolor( "51 79 192") || ///
line Mass period if xNass==6 , lcolor( "31 58 165") || ///
line Mass period if xNass==7 , lcolor( "17 41 137") || ///
line Mass period if xNass==8 , lcolor( "8 27 102") || ///
line Mass period if xNass==9 , lcolor( "2 14 63") ///
, ytitle("Orderliness index, {it:x}", size(medium)) ///
xtitle("", size(small)) ylabel(,labsize(small) nogrid) ysize(2.3) xsize(2.5) ///
xlabel(1 "1990-94" 2 "1995-98" 3 "1999-02" 4 "2003-06" 5 "2007-11" , angle(45) labsize(small) grid ) ///
legen(off) saving(trendMass, replace) graphregion(color(white))

********************
* 	Full Prof	   *
********************

use regFinal, clear
** 1.I take the nine SSD which are at the interior deciles (p10,p20,..., p80, p90) by size in period 1, and plot them over time
collapse Nass Kass mMassE Nord Kord mMordE, by(ssd period)
ren mMassE Mass
ren mMordE Mord

xtile xNord=Nord if period==1, nq(10)
egen tmp4Nord=count(Nord) if Nord!=., by(ssd)
gen tmp5Nord=tmp4Nord==5
egen tmp1Nord=max(Nord) if period==1&Nord!=.&tmp5Nord==1, by(xNord)
g tmp2Nord=(tmp1Nord==Nord) if Nord!=.
egen tmp3Nord=max(tmp2Nord), by(ssd)
egen sNord=sd(Nord), by(ssd)
egen tmp6Nord=min(sNord) if period==1&tmp3Nord==1&tmp5Nord==1, by(xNord)
g tmp7Nord=(sNord==tmp6Nord) if sNord!=.
egen tmp8Nord=max(tmp7Nord), by(ssd)
tab tmp8Nord
keep if tmp8Nord==1
egen tmp9Nord=max(xNord), by(ssd)
replace xNord=tmp9Nord
tab xNord, m

line Nord period if xNord==1 , lcolor( "249 140 140") || ///
line Nord period if xNord==2 , lcolor( "229 106 106") || ///
line Nord period if xNord==3 , lcolor( "249 140 140") || ///
line Nord period if xNord==4 , lcolor( "208 082 082") ||   ///
line Nord period if xNord==5 , lcolor( "188 054 054") ||   ///
line Nord period if xNord==6 , lcolor( "164 035 035") ||   ///
line Nord period if xNord==7 , lcolor( "127 021 021") ||   ///
line Nord period if xNord==8 , lcolor( "105 012 012") ||   ///
line Nord period if xNord==9 , lcolor( "073 004 004")       ///
, title("Associate professors")  ytitle("Number of competitors, {it:N}", size(medium)) ///
xtitle("", size(small)) yscale(log) ylabel(10 50 100 150  ,labsize(small) nogrid) ///
xlabel(none) xtick(1 (1) 5 , grid) ysize(2.3) xsize(2.5) ///
legen(off) saving(trendNord, replace) graphregion(color(white))

line Kord period if xNord==1 , lcolor( "249 140 140") || ///
line Kord period if xNord==2 , lcolor( "229 106 106") || ///
line Kord period if xNord==3 , lcolor( "249 140 140") || ///
line Kord period if xNord==4 , lcolor( "208 082 082") ||   ///
line Kord period if xNord==5 , lcolor( "188 054 054") ||   ///
line Kord period if xNord==6 , lcolor( "164 035 035") ||   ///
line Kord period if xNord==7 , lcolor( "127 021 021") ||   ///
line Kord period if xNord==8 , lcolor( "105 012 012") ||   ///
line Kord period if xNord==9 , lcolor( "073 004 004")       ///
, ytitle("Number of posts, {it:K}", size(medium)) ///
xtitle("", size(small)) yscale(log) ylabel( 5 10 20 40 ,labsize(small) nogrid) ///
xlabel(none) xtick(1 (1) 5 , grid) ysize(2.3) xsize(2.5) ///
legen(off) saving(trendKord, replace) graphregion(color(white))

line Mord period if xNord==1 , lcolor( "249 140 140") || ///
line Mord period if xNord==2 , lcolor( "229 106 106") || ///
line Mord period if xNord==3 , lcolor( "249 140 140") || ///
line Mord period if xNord==4 , lcolor( "208 082 082") ||   ///
line Mord period if xNord==5 , lcolor( "188 054 054") ||   ///
line Mord period if xNord==6 , lcolor( "164 035 035") ||   ///
line Mord period if xNord==7 , lcolor( "127 021 021") ||   ///
line Mord period if xNord==8 , lcolor( "105 012 012") ||   ///
line Mord period if xNord==9 , lcolor( "073 004 004")       ///
, ytitle("Orderliness index, {it:x}", size(medium)) ///
xtitle("", size(small)) ylabel(,labsize(small) nogrid) ysize(2.3) xsize(2.5) ///
xlabel(1 "1990-94" 2 "1995-98" 3 "1999-02" 4 "2003-06" 5 "2007-11" , angle(45) labsize(small) grid ) ///
legen(off) saving(trendMord, replace) graphregion(color(white))

* graph combine to get Figure A4 
graph combine trendNass.gph trendNord.gph trendKass.gph trendKord.gph trendMass.gph trendMord.gph, rows(3) col(2) ///
ysize(6.9) xsize(5) graphregion(color(white))
graph export "$path\fig_A4.pdf" , replace


** 1.ii plot average size (over the 5 periods) and sd (over the 5 periods) of the 380 SSD 
use regFinal, clear
set scheme s1mono
foreach var in N  {
	foreach fascia in ass{
	 preserve	
		collapse (mean) `var'`fascia'_avg=l`var'`fascia' (sd) `var'`fascia'_sd=l`var'`fascia', by(ssd)
				drop if `var'`fascia'_sd > 2
				twoway (scatter `var'`fascia'_sd `var'`fascia'_avg, msize(tiny)), title("1990-2011 - Assistant professors") yscale(range(0 2)) ytitle("Standard deviation of log({it:`var'})") xtitle("Average of log({it:`var'})") graphregion(color(white)) saving(msd`var'`fascia', replace)
	 restore
	}
}
foreach var in N  {
	foreach fascia in ord{
	 preserve	
		collapse (mean) `var'`fascia'_avg=l`var'`fascia' (sd) `var'`fascia'_sd=l`var'`fascia', by(ssd)
				drop if `var'`fascia'_sd > 2
			twoway (scatter `var'`fascia'_sd `var'`fascia'_avg, msize(tiny)), title("1990-2011 - Associate professors") ytitle("Standard deviation of log({it:`var'})") xtitle("Average of log({it:`var'})") graphregion(color(white))  saving(msd`var'`fascia', replace)
	 restore
	}
}

foreach fascia in  ord{
	 preserve	
		collapse (mean) mM`fascia'E_avg=mM`fascia'E (sd) mM`fascia'E_sd=mM`fascia'E, by(ssd)
				drop if mM`fascia'E_avg < -0.5
				drop if mM`fascia'E_sd > 0.8
				twoway (scatter mM`fascia'E_sd mM`fascia'E_avg, msize(tiny)),   ytitle("Standard deviation of {it:x}") xtitle("Average of {it:x}") graphregion(color(white))  saving(msd_x`fascia', replace)
	 restore
}


foreach var in  K {
	foreach fascia in ass{
	 preserve	
		collapse (mean) `var'`fascia'_avg=l`var'`fascia' (sd) `var'`fascia'_sd=l`var'`fascia', by(ssd)
				drop if `var'`fascia'_sd > 2
				twoway (scatter `var'`fascia'_sd `var'`fascia'_avg, msize(tiny)),  yscale(range(0 2)) ytitle("Standard deviation of log({it:`var'})") xtitle("Average of log({it:`var'})") graphregion(color(white)) saving(msd`var'`fascia', replace)
	 restore
	}
}
foreach var in  K {
	foreach fascia in ord{
	 preserve	
		collapse (mean) `var'`fascia'_avg=l`var'`fascia' (sd) `var'`fascia'_sd=l`var'`fascia', by(ssd)
				drop if `var'`fascia'_sd > 2
			twoway (scatter `var'`fascia'_sd `var'`fascia'_avg, msize(tiny)),  ytitle("Standard deviation of log({it:`var'})") xtitle("Average of log({it:`var'})") graphregion(color(white))  saving(msd`var'`fascia', replace)
	 restore
	}
}

foreach fascia in ass {
	 preserve	
		collapse (mean) mM`fascia'E_avg=mM`fascia'E (sd) mM`fascia'E_sd=mM`fascia'E, by(ssd)
				drop if mM`fascia'E_avg < -0.5
				drop if mM`fascia'E_sd > 0.8
				twoway (scatter mM`fascia'E_sd mM`fascia'E_avg, msize(tiny)),   ytitle("Standard deviation of {it:x}") xtitle("Average of {it:x}") graphregion(color(white))  saving(msd_x`fascia', replace)
	 restore
}


graph combine  msdNass.gph msdNord.gph  , rows(1) col(2) ///
graphregion(color(white)) ycom saving(gN, replace) ysize(3)

graph combine  msdKass.gph msdKord.gph , rows(1) col(2) ///
graphregion(color(white)) ycom saving(gK, replace) ysize(2.2)

graph combine  msd_xass.gph msd_xord.gph , rows(1) col(2) ///
graphregion(color(white)) ycom saving(gM, replace) ysize(2.2)

* graph combine to get Figure A5
graph combine  gN.gph gK.gph gM.gph ///
, graphregion(color(white)) rows(3) col(1) ysize(7.5)
graph export "$path\fig_A5.pdf" , replace

************************************************************************
***				  Table A11 columns 9-10 						     ***
************************************************************************
** Same regressions as columns 3 and 4 in main Table but where N includes 
*  those appointed from outside

* after running lines 74-88 of datacdv2.do to generate new_prof.dta run the following
clear mata
clear matrix
set maxvar 32767
use regFinal , clear
merge m:1 ssd period using new_prof.dta, keep(1 3)

drop lNass lNord lnNass lnNord

* redefine N including also people getting a position from abroad
gen lNass=log(Nass+newass)
gen lNord=log(Nord+neword)

**** generate two new continuous variables   ******
foreach fascia in ass ord{
	gen lnN`fascia' =  lN`fascia'*refrule`fascia'
}

set more off
global tavola = "c.mM#i.quartile1 c.lK#i.quartile1 c.lN#i.quartile1 c.lnN#i.quartile1  ext avage shfemale homo1 homo3 p2-p5 "
foreach fascia in ass ord{
foreach cluster in   ssd {
preserve 
	ren quartile1e quartile1
	ren mM`fascia'E mM 
	ren lK`fascia'  lK
	ren lkK`fascia'  lkK
	ren lnN`fascia'  lnN
	ren lN`fascia'  lN
	ren ext`fascia' ext
	ren refrule`fascia' refrule
	global cond = "(fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0"
	xtreg ioutput1e $tavola sd1-sd369 uni1-uni90 if $cond , fe vce(cluster  `cluster') /* dfadj */ 
	parmest, saving("beta_`fascia'.dta", replace)
	predict indiv_fe_`fascia', u
	outreg2 using tabA11.xls, keep($tavola) noparen aster(coef) se bdec(3) nocons cti(`fascia' output1e) label append
	save tmp_`fascia', replace
restore
	}
}

* run datacdv2.do line 74-88 to get the new table with changes by period and fascia: Rank_Promotions_By_Period.xlsx

************************************************************************
***			 Appendix: Section E3 pag A25 and A26  				     ***
************************************************************************
* Test if the Parallel Trend Assumption is valid 
foreach fascia in ass ord{
	preserve
	collapse ioutput1e refrule`fascia' if (fasc`fascia'>0|`fascia'_1==1) & e`fascia'==0, by(ssd period)
	table period, c(m refrule`fascia' sd refrule`fascia')
	xtset ssd period 
	gen delta=(ioutput1e-L.ioutput1e)/L.ioutput1e
	replace delta=. if period>2
	collapse (mean) delta refrule`fascia' , by(ssd)
	su delta refrule`fascia'
	rename refrule`fascia' R
	label var R "R"
	reg delta R
	outreg2 using parallel_trend_test_f.xls, noparen aster(coef) se bdec(3) cti(`fascia') label append
	su R, detail
	gen dR=R>`r(p50)'
	rename R refrule`fascia'
	rename dR R
	reg delta R
	outreg2 using parallel_trend_test.xls, noparen aster(coef) se bdec(3) cti("`fascia' dummy") label append
	restore
}


